﻿using System.Collections.Generic;
using System.Web;

namespace BitworkStudios.Web.Mvc.Framework.Navigation
{
    public class Menu
    {
        public Menu(SiteMapNode rootSiteMapNode)
        {
            RootMenu = new List<MenuItemNode>();
            RootSiteMapNode = rootSiteMapNode;
        }

        private SiteMapNode _rootSiteMapNode;
        public SiteMapNode RootSiteMapNode
        {
            set
            {
                _rootSiteMapNode = value;
                PopulateMenuItemNodes();
            }
        }

        public List<MenuItemNode> RootMenu { get; set; }

        private SiteMapNode _currentSiteMapNode;

        public SiteMapNode CurrentSiteMapNode
        {
            set
            {
                _currentSiteMapNode = value;
                if (_currentSiteMapNode.ParentNode != null)
                {
                    if (_currentSiteMapNode.ParentNode.Title == "Root")
                    {
                        foreach (MenuItemNode menuItemNode in RootMenu)
                        {
                            if (menuItemNode.Url.Url == _currentSiteMapNode.Url)
                            {
                                CurrentMenuItemNode = menuItemNode;
                                break;
                            }
                        }
                    }
                    else if (_currentSiteMapNode.ParentNode.ParentNode.Title == "Root")
                    {
                        foreach (MenuItemNode menuItemNode in RootMenu)
                        {
                            if (menuItemNode.Url.Url == _currentSiteMapNode.ParentNode.Url)
                            {
                                foreach (var sectionNode in menuItemNode.ChildNodes)
                                {
                                    if (sectionNode.Url.Url == _currentSiteMapNode.Url)
                                    {
                                        CurrentMenuItemNode = sectionNode;
                                        break;
                                    }
                                }
                                break;
                            }
                        }
                    }
                    else if (_currentSiteMapNode.ParentNode.ParentNode.ParentNode.Title == "Root")
                    {
                        foreach (MenuItemNode menuItemNode in RootMenu)
                        {
                            if (menuItemNode.Url.Url == _currentSiteMapNode.ParentNode.ParentNode.Url)
                            {
                                foreach (var sectionNode in menuItemNode.ChildNodes)
                                {
                                    if (sectionNode.Url.Url == _currentSiteMapNode.ParentNode.Url)
                                    {
                                        foreach (var sideNode in sectionNode.ChildNodes)
                                        {
                                            if (sideNode.Url.Url == _currentSiteMapNode.Url)
                                            {
                                                CurrentMenuItemNode = sideNode;
                                                break;
                                            }
                                        }
                                        break;
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }

        private MenuItemNode _currentMenuItemNode;
        public MenuItemNode CurrentMenuItemNode
        {
            get { return _currentMenuItemNode; }
            set
            {
                _currentMenuItemNode = value;
                ClearActiveStates(_currentMenuItemNode);
                SetActiveState(_currentMenuItemNode);
            }
        }

        public List<MenuItemNode> SectionMenu
        {
            get { return GetSectionMenu(); }
        }

        public List<MenuItemNode> SideMenu
        {
            get { return GetSideMenu(); }
        }

        private List<MenuItemNode> GetSectionMenu()
        {
            if (_currentMenuItemNode.ParentNode == null)
            {
                return _currentMenuItemNode.ChildNodes;
            }

            if(_currentMenuItemNode.ParentNode.ParentNode == null)
            {
                return _currentMenuItemNode.ParentNode.ChildNodes;
            }

            if (_currentMenuItemNode.ParentNode.ParentNode.ParentNode == null)
            {
                return _currentMenuItemNode.ParentNode.ParentNode.ChildNodes;
            }

            return new List<MenuItemNode>();
        }

        private List<MenuItemNode> GetSideMenu()
        {

            if (_currentMenuItemNode.ParentNode != null)
            {
                if (_currentMenuItemNode.ParentNode.ParentNode == null)
                {
                    return _currentMenuItemNode.ChildNodes;
                }

                if (_currentMenuItemNode.ParentNode.ParentNode.ParentNode == null)
                {
                    return _currentMenuItemNode.ParentNode.ChildNodes;
                }

                return _currentMenuItemNode.ChildNodes;
            }

            return new List<MenuItemNode>();
        }

        public void ClearActiveStates(MenuItemNode node)
        {
            foreach (MenuItemNode menuItemNode in RootMenu)
            {
                menuItemNode.IsActive = false;
            }
        }

        public void SetActiveState(MenuItemNode node)
        {
            _currentMenuItemNode.IsActive = true;
            if (_currentMenuItemNode.ParentNode != null)
            {

                if (_currentMenuItemNode.ParentNode.ParentNode == null)
                {
                    _currentMenuItemNode.ParentNode.IsActive = true;
                }

                if (_currentMenuItemNode.ParentNode.ParentNode != null)
                {
                    if (_currentMenuItemNode.ParentNode.ParentNode.ParentNode == null)
                    {
                        _currentMenuItemNode.ParentNode.IsActive = true;
                        _currentMenuItemNode.ParentNode.ParentNode.IsActive = true;
                    }
                }
            }
        }

        private void PopulateMenuItemNodes()
        {
            foreach (SiteMapNode siteMapNode in _rootSiteMapNode.ChildNodes)
            {
                var menuItemNode = new MenuItemNode
                                       {
                                           Title = siteMapNode.Title, 
                                           Url = new UrlContext(siteMapNode.Url)
                                       };
                RootMenu.Add(menuItemNode);
                foreach (SiteMapNode sectionNode in siteMapNode.ChildNodes)
                {
                    var sectionMenuItemNode = new MenuItemNode
                                                  {
                                                      Title = sectionNode.Title, 
                                                      Url = new UrlContext(sectionNode.Url),
                                                      ParentNode = menuItemNode
                                                  };
                    menuItemNode.ChildNodes.Add(sectionMenuItemNode);
                    foreach (SiteMapNode sideNode in sectionNode.ChildNodes)
                    {
                        var sideMenuItemNode = new MenuItemNode
                                                   {
                                                       Title = sideNode.Title, 
                                                       Url = new UrlContext(sideNode.Url),
                                                       ParentNode = sectionMenuItemNode
                                                   };
                        sectionMenuItemNode.ChildNodes.Add(sideMenuItemNode);
                    }
                }
            }
        }
    }
}
